<template>
  <div class="login_container">
      <!-- 登录框 -->
      <div class="login-box">
          <!-- 头像区域 -->
          <div class="login-avatar">
              <img src="../assets/logopic.png" class="avatar-pic">
          </div>
          <!-- 登录表单区域 -->
          <el-form ref="loginFormRef" :model="loginForm" class="login-form" :rules="loginFormRules">
            <el-form-item prop="username">
            <el-input v-model.trim="loginForm.username" placeholder="登录账号 / 邮箱 / 手机号码" prefix-icon="iconfont icon-user"></el-input>
            </el-form-item>
            <el-form-item prop="password">
            <el-input v-model.trim="loginForm.password" prefix-icon="iconfont icon-3702mima" placeholder="请输入密码" type="password"></el-input>
            </el-form-item>
            <el-form-item class="btn-right">
                <el-button type="primary" @click="login">登录</el-button>
                <el-button type="info" @click="resetLoginForm">重置</el-button>
            </el-form-item>
          </el-form>
      </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: 'blur' },
          { min: 3, max: 6, message: '密码长度应为3 — 6位数字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 表单重置
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    // 表单提交
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return this.$message.error('对不起，信息不合法是我们最远的距离...')
        const { data: res } = await this.$http.post('/login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$notify({
          title: `${res.data.username}： Welcome Back~`,
          message: '思念,是山涧的风,回旋不已,无孔不入...',
          offset: 100,
          type: 'success'
        })
        sessionStorage.setItem('username', res.data.username)
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b5b6b;
  height: 100%;
  .login-box {
      position:absolute;
      left: 50%;
      top: 50%;
      width: 450px;
      height: 300px;
      transform: translate(-50%,-50%);
      background-color: #fff;
      border-radius: 8px;
      box-shadow: 0 0 7px 0 #dddddd;
      .login-avatar{
          position: absolute;
          left: 50%;
          transform: translate(-50%,-50%);
          background-color: #ffffff;
          width: 130px;
          height: 130px;
          padding: 8px;
          border-radius: 50%;
          box-shadow: 0 0 10px 0 #dddddd;
          .avatar-pic {
              position: absolute;
              top: 50%;
              left: 50%;
              transform: translate(-50%, -50%);
              width: 100%;
          }
      }
      .login-form{
          position: absolute;
          padding: 0 14px;
          box-sizing: border-box;
          width: 100%;
          bottom: 10px;
          .btn-right {
              display: flex;
              justify-content: flex-end;
          }
      }
  }
}

</style>
